package org.microsleep;

import java.lang.reflect.Method;
import java.util.EnumSet;

public class Property {
	private String name;
	private Type type;
	private int length;
	private Method getter;
	private Method setter;
	private Class<?> javaType;
	private FetchMode fetchMode;
	private EnumSet<CascadeOperation> cascades;

	public void setType(Type type) {
		this.type = type;
	}

	public Type getType() {
		return type;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getLength() {
		return length;
	}

	public void setGetter(Method getter) {
		this.getter = getter;
	}

	public Object getValue(Object from) {
		Util.notNull(from);
		try {
			return getter.invoke(from, (Object[]) null);
		} catch (Exception e) {
			throw new RuntimeException("Getter for property " + getName()
					+ " not accessible", e);
		}
	}

	public void setSetter(Method setter) {
		this.setter = setter;
	}

	public void setValue(Object on, Object value) {
		Util.notNull(on);
		try {
			setter.invoke(on, value);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void setJavaType(Class<?> javaType) {
		this.javaType = javaType;
	}

	public Class<?> getJavaType() {
		return javaType;
	}

	public void setFetchMode(FetchMode fetchMode) {
		this.fetchMode = fetchMode;
	}

	public FetchMode getFetchMode() {
		return fetchMode;
	}

	public void setCascades(EnumSet<CascadeOperation> cascades) {
		this.cascades = cascades;
	}

	public EnumSet<CascadeOperation> getCascades() {
		return cascades;
	}

}